﻿package morn.core.components
{
    import flash.display.*;
    import morn.core.events.*;
    import morn.core.handlers.*;
    import morn.core.utils.*;

    public class Image extends Component implements ISkinAbleContainer
    {
        protected var _bitmap:ImageBitmapData;
        protected var _url:String;
        private var mPivotX:Number = 0;
        private var mPivotY:Number = 0;
        protected var _skinChanged:Boolean = false;

        public function Image(param1:String = null)
        {
            this.url = param1;
            return;
        }// end function

        public function set skinChanged(param1:Boolean) : void
        {
            this._skinChanged = param1;
            return;
        }// end function

        public function setSkin(param1:Object) : void
        {
            if (!this._skinChanged)
            {
                this.url = param1 as String;
            }
            return;
        }// end function

        public function get pivotX() : Number
        {
            return this.mPivotX;
        }// end function

        public function set pivotX(param1:Number) : void
        {
            if (this.mPivotX != param1)
            {
                this.mPivotX = param1;
                if (this._bitmap)
                {
                    this._bitmap.x = -this.mPivotX;
                }
            }
            return;
        }// end function

        public function get pivotY() : Number
        {
            return this.mPivotY;
        }// end function

        public function set pivotY(param1:Number) : void
        {
            if (this.mPivotY != param1)
            {
                this.mPivotY = param1;
                if (this._bitmap)
                {
                    this._bitmap.y = -this.mPivotY;
                }
            }
            return;
        }// end function

        override protected function createChildren() : void
        {
            this._bitmap = new ImageBitmapData(this);
            this._bitmap.x = -this.mPivotX;
            this._bitmap.y = -this.mPivotY;
            return;
        }// end function

        public function get url() : String
        {
            return this._url;
        }// end function

        public function set url(param1:String) : void
        {
            if (this._url != param1)
            {
                this._url = param1;
                if (Boolean(param1))
                {
                    if (this._bitmap)
                    {
                        if (!this._bitmap.enabled)
                        {
                            this.createChildren();
                        }
                        this._bitmap.url = param1;
                    }
                    if (App.asset.hasClass(this._url))
                    {
                        this.bitmapData = App.asset.getBitmapData(this._url);
                    }
                    else
                    {
                        App.mloader3.loadBMD(this._url, 2, new Handler(this.setBitmapData, [this._url]), null, null, false);
                    }
                }
                else
                {
                    this.bitmapData = null;
                }
            }
            this._skinChanged = true;
            return;
        }// end function

        public function get bitmapData() : BitmapData
        {
            return this._bitmap.bitmapData;
        }// end function

        public function set bitmapData(param1:BitmapData) : void
        {
            if (param1)
            {
                _contentWidth = param1.width;
                _contentHeight = param1.height;
            }
            this._bitmap.bitmapData = param1;
            sendEvent(UIEvent.IMAGE_LOADED);
            return;
        }// end function

        protected function setBitmapData(param1:String, param2:BitmapData) : void
        {
            if (param1 == this._url)
            {
                this.bitmapData = param2;
            }
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            super.width = param1;
            this._bitmap.width = width;
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            super.height = param1;
            this._bitmap.height = height;
            return;
        }// end function

        public function get sizeGrid() : String
        {
            if (this._bitmap.sizeGrid)
            {
                return this._bitmap.sizeGrid.join(",");
            }
            return null;
        }// end function

        public function set sizeGrid(param1:String) : void
        {
            this._bitmap.sizeGrid = StringUtils.fillArray(Styles.defaultSizeGrid, param1, int);
            return;
        }// end function

        public function get bitmap() : ImageBitmapData
        {
            return this._bitmap;
        }// end function

        public function get smoothing() : Boolean
        {
            return this._bitmap.smoothing;
        }// end function

        public function set smoothing(param1:Boolean) : void
        {
            this._bitmap.smoothing = param1;
            return;
        }// end function

        override public function set dataSource(param1:Object) : void
        {
            _dataSource = param1;
            if (param1 is String)
            {
                this.url = String(param1);
            }
            else
            {
                super.dataSource = param1;
            }
            return;
        }// end function

        public function dispose(param1:Boolean = false) : void
        {
            this._bitmap.dispose();
            App.asset.disposeBitmapData(this._url);
            if (param1)
            {
                App.mloader3.clearResLoaded(this._url);
            }
            return;
        }// end function

    }
}
